﻿package njClass.bat{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	public class MousePlus {
		private static var rootStage:Sprite;
		private static var color:uint=0xFFFFFF;
		private static var BallSize:Number;
		private static var GlowSpeed:int;
		
		//鼠标点击出现Obj
		public static function MouseGlowBall(Obj:Sprite,Size:Number=5,Dcolor:uint=0xFFFFFF,speed:int=2):void {
			rootStage=Obj;
			BallSize=Size;
			color=Dcolor;
			GlowSpeed=speed;
			Obj.stage.addEventListener(MouseEvent.MOUSE_DOWN,startTimer);
			
			function startTimer(e:MouseEvent):void {
				//trace("start");
				Obj.stage.addEventListener(MouseEvent.MOUSE_UP,stopTimer);
				var timer:Timer=new Timer(100);
				timer.addEventListener(TimerEvent.TIMER,showGlowBall);
				timer.reset();
				timer.start();
				
				function stopTimer(e:MouseEvent):void {
					Obj.stage.removeEventListener(MouseEvent.MOUSE_UP,stopTimer);
					timer.stop();
				}
			}
		}
		
		private static function showGlowBall(e:TimerEvent):void {
			var tempBall:Sprite=new Sprite();
			var LightStrength:Number=10;
			var filterAlpha:Number=1;
			var filter:GlowFilter=new GlowFilter(color,filterAlpha,8,8,LightStrength,BitmapFilterQuality.HIGH);
			tempBall.addChild(doDrawCircle(color,BallSize));
			tempBall.x=rootStage.mouseX+Math.floor(Math.random()*5-5);
			tempBall.y=rootStage.mouseY+Math.floor(Math.random()*5-5);
			tempBall.filters=[filter];
			MovePlus.ramdonMove(tempBall);
			rootStage.addChild(tempBall);
			var FilterTimer:Timer=new Timer(100,40);
			FilterTimer.addEventListener(TimerEvent.TIMER,changeBall);
			FilterTimer.start();
			var timer:Timer=new Timer(4000,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,removeBall);
			timer.start();
			
			function removeBall(e:TimerEvent):void {
				rootStage.removeChild(tempBall);
				//e.updateAfterEvent();
			}
			
			function changeBall(e:TimerEvent):void {
				tempBall.scaleX=tempBall.scaleX*0.95;
				tempBall.scaleY=tempBall.scaleY*0.95;
				tempBall.alpha-=0.04;
				filter=new GlowFilter(color,filterAlpha,8,8,LightStrength,BitmapFilterQuality.HIGH);
				tempBall.filters=[filter];
				//e.updateAfterEvent();
			}
		}
		
		private static function doDrawCircle(bgColor:uint=0xFFFFFF,halfSize:Number=5):Shape {
			var child:Shape = new Shape();
			child.graphics.beginFill(bgColor);
			child.graphics.drawCircle(0, 0, int(Math.random()*halfSize/2)+halfSize/2);
			child.graphics.endFill();
			return child;
		}
		
		//鼠标样子替换
		public static function replaceMouse(Obj:*,bg:Sprite):void {
			Mouse.hide();
			var mouse:DisplayObject=new Obj();
			bg.addChild(mouse);
			bg.stage.addEventListener(MouseEvent.MOUSE_MOVE,updateMouse);

			function updateMouse(e:MouseEvent):void {
				mouse.x=bg.mouseX;
				mouse.y=bg.mouseY;
				e.updateAfterEvent();
			}
		}
	}
}